package top.jafar.data.structures.expression;

public abstract class ExpressionOperator<T> {

    /**
     * 计算表达式并返回结果
     * @param expression
     * @return
     */
    public abstract T execute(String expression);

    public abstract T doCalculation(char op, String ...args);

    /**
     * 由中缀表达式获取逆波兰表达式
     * @param express
     * @return
     */
    protected abstract String getAntiPolishNotationExpress(String express);

    /**
     * 是否操作符
     * @param ch
     * @return
     */
    protected abstract boolean isOperator(char ch);
    protected abstract boolean isOperator(String ch);


    /**
     * 是否括号
     * @param operator
     * @return
     */
    public boolean isBrackets(char operator) {
        return isLeftBrackets(operator) || isRightBrackets(operator);
    }
    public boolean isBrackets(String operator) {
        return isLeftBrackets(operator) || isRightBrackets(operator);
    }

    /**
     * 是否左括号
     * @param operator
     * @return
     */
    public boolean isLeftBrackets(char operator) {
        return operator == '(';
    }
    public boolean isLeftBrackets(String operator) {
        return "(".equals(operator);
    }

    /**
     * 是否右括号
     * @param operator
     * @return
     */
    public boolean isRightBrackets(char operator) {
        return operator == ')';
    }
    public boolean isRightBrackets(String operator) {
        return ")".equals(operator);
    }

    /**
     * 是否逻辑符号
     * @param operator
     * @return
     */
    public boolean isLogicOperator(char operator) {
        return isAndOperator(operator) || isOrOperator(operator);
    }

    public boolean isAndOperator(char op) {
        return op == '&';
    }

    public boolean isOrOperator(char op) {
        return op == '|';
    }
}